﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;


namespace TSP
{
    public class MainClass
    {
        private float _alpha1, _alpha2, _beta;      
           
        private void geneticAlgorithm(string[] args)
        {            
            if((args == null) || (args.Length == 0))
            {
                Console.WriteLine("Khong co tham so");
            }
            else
            {
                //GA: giai_thuat(0); ten_file_du_lieu(1); so_lan_danh_gia(2); ti_le_lai_ghep(3); kich_thuoc_quan_the_ban_dau(4); tourament_size(5); elitis(6); seed(7);
                Console.WriteLine("* File du lieu: " + args[1]);                                
                InOutData.readFile(args[1]);                
                Population pop = new Population(int.Parse(args[2]), float.Parse(args[3]), int.Parse(args[4]), int.Parse(args[5]), Convert.ToBoolean(args[6]), int.Parse(args[7]));
                pop.writeResult = new Population.WriteResulToFile(InOutData.writeFile);               
                pop.writeFitness = new Population.WriteFitnessToFile(InOutData.writeFitnessInfo);
                string saveFile = "";  
                           
                Stopwatch stGA = new Stopwatch();                
                stGA.Start();
                switch (Convert.ToInt32(args[0]))
                {
                    case 1: 
                        saveFile  = "GA_MSCX_" + args[1] + "_evaluate(" + args[2] + ")_crossRate(" + args[3] + ")_popSize(" + args[4] + ")_touranmentSize(" + args[5] + ")_seed(" + args[7] + ")";
                        pop.FileName = saveFile + @".info";
                        Console.WriteLine("** GA_MSCX_" + "seed: " + args[7]);
                        pop.GA_MSCX();
                        break;
                    case 2: 
                        saveFile  = "GA_SCX_" + args[1] + "_evaluate(" + args[2] + ")_crossRate(" + args[3] + ")_popSize(" + args[4] + ")_touranmentSize(" + args[5] + ")_seed(" + args[7] + ")";
                        pop.FileName = saveFile + @".info";
                        Console.WriteLine("** GA_SCX" + "seed: " + args[7]);
                        pop.GA_SCX();
                        break;
                    case 3: 
                        saveFile  = "GA_MOX_" + args[1] + "_evaluate(" + args[2] + ")_crossRate(" + args[3] + ")_popSize(" + args[4] + ")_touranmentSize(" + args[5] + ")_seed(" + args[7] + ")";
                        pop.FileName = saveFile + @".info";
                        Console.WriteLine("** GA_MOX" + "seed: " + args[7]);
                        pop.GA_MOX();
                        break;
                    case 4: 
                        saveFile  = "GA_OX_" + args[1] + "_evaluate(" + args[2] + ")_crossRate(" + args[3] + ")_popSize(" + args[4] + ")_touranmentSize(" + args[5] + ")_seed(" + args[7] + ")";
                        pop.FileName = saveFile + @".info";
                        Console.WriteLine("** GA_OX" + "seed: " + args[7]);
                        pop.GA_OX();
                        break;
                    case 5:
                        saveFile = "GA_M_MSCX_" + args[1] + "_evaluate(" + args[2] + ")_crossRate(" + args[3] + ")_popSize(" + args[4] + ")_touranmentSize(" + args[5] + ")_seed(" + args[7] + ")";
                        pop.FileName = saveFile + @".info";
                        Console.WriteLine("** GA_M_MSCX_" + "seed: " + args[7]);
                        pop.GA_M_MSCX();
                        break;
                    case 6:
                        saveFile = "GA_M_MSCX1_1_" + args[1] + "_evaluate(" + args[2] + ")_crossRate(" + args[3] + ")_popSize(" + args[4] + ")_touranmentSize(" + args[5] + ")_seed(" + args[7] + ")";
                        pop.FileName = saveFile + @".info";
                        Console.WriteLine("** GA_M_MSCX1_1_" + "    seed: " + args[7]);
                        pop.GA_M_MSCX1_1();
                        break;
                    case 7:
                        saveFile = "GA_M_MSCX2_" + args[1] + "_evaluate(" + args[2] + ")_crossRate(" + args[3] + ")_popSize(" + args[4] + ")_touranmentSize(" + args[5] + ")_seed(" + args[7] + ")";
                        pop.FileName = saveFile + @".info";
                        Console.WriteLine("** GA_M_MSCX2_" + "seed: " + args[7]);
                        pop.GA_M_MSCX2();
                        break;
                    case 8:
                        saveFile = "GA_M_MSCX3_" + args[1] + "_evaluate(" + args[2] + ")_crossRate(" + args[3] + ")_popSize(" + args[4] + ")_touranmentSize(" + args[5] + ")_seed(" + args[7] + ")";
                        pop.FileName = saveFile + @".info";
                        Console.WriteLine("** GA_M_MSCX3_" + "seed: " + args[7]);
                        pop.GA_M_MSCX3();
                        break;
                    case 9:
                        saveFile = "GA_M_MSCX4_" + args[1] + "_evaluate(" + args[2] + ")_crossRate(" + args[3] + ")_popSize(" + args[4] + ")_touranmentSize(" + args[5] + ")_seed(" + args[7] + ")";
                        pop.FileName = saveFile + @".info";
                        Console.WriteLine("** GA_M_MSCX4_" + "seed: " + args[7]);
                        pop.GA_M_MSCX4();
                        break;
                    case 10:
                        saveFile = "GA_M_MSCX5_" + args[1] + "_evaluate(" + args[2] + ")_crossRate(" + args[3] + ")_popSize(" + args[4] + ")_touranmentSize(" + args[5] + ")_seed(" + args[7] + ")";
                        pop.FileName = saveFile + @".info";
                        Console.WriteLine("** GA_M_MSCX5_" + "seed: " + args[7]);
                        pop.GA_M_MSCX5();
                        break;
                    case 11:
                        saveFile = "GA_M_MSCX6_" + args[1] + "_evaluate(" + args[2] + ")_crossRate(" + args[3] + ")_popSize(" + args[4] + ")_touranmentSize(" + args[5] + ")_seed(" + args[7] + ")";
                        pop.FileName = saveFile + @".info";
                        Console.WriteLine("** GA_M_MSCX6_" + "seed: " + args[7]);
                        pop.GA_M_MSCX6();
                        break;
                    case 12:
                        saveFile = "GA_M_MSCX7_2_" + args[1] + "_evaluate(" + args[2] + ")_crossRate(" + args[3] + ")_popSize(" + args[4] + ")_touranmentSize(" + args[5] + ")_seed(" + args[7] + ")";
                        pop.FileName = saveFile + @".info";
                        Console.WriteLine("** GA_M_MSCX7_2_" + "seed: " + args[7]);
                        pop.GA_M_MSCX7();
                        break;
                    case 13:
                        saveFile = "GA_LinearDistace_" + args[1] + "_evaluate(" + args[2] + ")_crossRate(" + args[3] + ")_popSize(" + args[4] + ")_touranmentSize(" + args[5] + ")_seed(" + args[7] + ")";
                        pop.FileName = saveFile + @".info";
                        Console.WriteLine("** GA_LinearDistace__" + "seed: " + args[7]);
                        pop.GA_LinearDistance();
                        break;
                    case 14:
                        float.TryParse(args[8], out _alpha1);
                        float.TryParse(args[9], out _alpha2);
                        float.TryParse(args[10], out _beta);
                        saveFile = "GA_LinearDistace_1_" + args[1] + "_evaluate(" + args[2] + ")_crossRate(" + args[3] + ")_popSize(" + args[4] + ")_touranmentSize(" + args[5] + ")_seed(" + args[7] + ")_alpha1(" + args[8]
                            + ")_alpha2(" + args[9] + ")_beta(" + args[10] + ")";
                        pop.FileName = saveFile + @".info";
                        Console.WriteLine("** GA_LinearDistace__1 " + "seed(" + args[7] + ")_alpha1(" + args[8] + ")_alpha2(" + args[9] + ")_beta(" + args[10] + ")");
                        pop.GA_LinearDistance1(_alpha1, _alpha2, _beta);
                        break;              
                    case 15:
                        int radius = 0;
                        int.TryParse(args[8], out radius);                        
                        saveFile = "GA_MSCX_RadiusDistance_" + args[1] + "_evaluate(" + args[2] + ")_crossRate(" + args[3] + ")_popSize(" + args[4] + ")_touranmentSize(" + args[5] + ")_seed(" + args[7] + ")_Radius(" + args[8] + ")";
                        pop.FileName = saveFile + @".info";
                        Console.WriteLine("** GA_Radius " + "seed(" + args[7] + ")_radius(" + args[8] + ")");
                        pop.GA_RadiusDistance(radius);
                        break;
                    case 16:                       
                        saveFile = "GA_MSCX_Hybridizing_" + args[1] + "_evaluate(" + args[2] + ")_crossRate(" + args[3] + ")_popSize(" + args[4] + ")_touranmentSize(" + args[5] + ")_seed(" + args[7] + ")";
                        pop.FileName = saveFile + @".info";
                        Console.WriteLine("** GA_Hybridizing " + "seed(" + args[7] + ")");
                        pop.GA_Hybridizing();
                        break;
                    case 17:
                        saveFile = "GA_MSCX_Hybridizing 1_" + args[1] + "_evaluate(" + args[2] + ")_crossRate(" + args[3] + ")_popSize(" + args[4] + ")_touranmentSize(" + args[5] + ")_seed(" + args[7] + ")";
                        pop.FileName = saveFile + @".info";
                        Console.WriteLine("** GA_Hybridizing 1 " + "seed(" + args[7] + ")");
                        pop.GA_Hybridizing();
                        break;
                    case 18:
                        saveFile = "GA_MSCX_TwoChidren_" + args[1] + "_evaluate(" + args[2] + ")_crossRate(" + args[3] + ")_popSize(" + args[4] + ")_touranmentSize(" + args[5] + ")_seed(" + args[7] + ")";
                        pop.FileName = saveFile + @".info";
                        Console.WriteLine("** GA_MSCX_TwoChidren_ " + "seed(" + args[7] + ")");
                        pop.GA_Hybridizing();
                        break;
                    case 19:
                        saveFile = "GA_MSCX_PMX_" + args[1] + "_evaluate(" + args[2] + ")_crossRate(" + args[3] + ")_popSize(" + args[4] + ")_touranmentSize(" + args[5] + ")_seed(" + args[7] + ")";
                        pop.FileName = saveFile + @".info";
                        Console.WriteLine("** GA_MSCX_PMX_ " + "seed(" + args[7] + ")");
                        pop.GA_MSCX_PMX();
                        break;
                    case 20:
                        saveFile = "GA_Hybridizing_" + args[1] + "_evaluate(" + args[2] + ")_crossRate(" + args[3] + ")_popSize(" + args[4] + ")_touranmentSize(" + args[5] + ")_seed(" + args[7] + ")";
                        pop.FileName = saveFile + @".info";
                        Console.WriteLine("** GA_Hybridizing_ " + "seed(" + args[7] + ")");
                        pop.GA_HyBridizing_MSCX7();
                        break;
                }
                stGA.Stop();
                saveFile += @"_Final.tour";
                pop.writeResult(saveFile, Population.BestChromosome.chromo, Population.BestChromosome.Fitness, stGA);
                Population.BestChromosome = null;
                Console.WriteLine("\t + Thoi gian chay: {0} giay", stGA.Elapsed.ToString());
                Console.WriteLine("\t + OK");         
            }           
        }
       
        public void run(string[] args)
        {            
            Console.WriteLine("-----------START-----------");
            geneticAlgorithm(args);
           
            Console.WriteLine("-----------FINISH-----------");            
        }//public void run(int idx)

    }//public class MainClass
}
